package com.qyhl.webtv.module_microvideo.shortvideo.play.player;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.microvideo.ShortVideoBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.module_microvideo.R;

import java.util.ArrayList;
import java.util.List;

public class QXVideoAdapter extends RecyclerView.Adapter {
    private List<ShortVideoBean> dataList = new ArrayList<>();

    private Context mContext;

    private RecyclerView recyclerView;

    private OnItemChildClickListener onItemChildClickListener;

    public void setOnItemChildClickListener(OnItemChildClickListener onItemChildClickListener) {
        this.onItemChildClickListener = onItemChildClickListener;
    }

    public QXVideoAdapter(Context context, RecyclerView recyclerView) {
        this.mContext = context;
        this.recyclerView = recyclerView;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.micro_item_short_video_play_qx, parent, false);
        return new VideoViewHolder(view);
    }


    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ShortVideoBean videoBean = dataList.get(position);
        final VideoViewHolder viewHolder = (VideoViewHolder) holder;

        Glide.with(mContext)
                .asBitmap()
                .load(videoBean.getLogo())
                .listener(new RequestListener<Bitmap>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                        int width = resource.getWidth();
                        int height = resource.getHeight();
                        fitVideoScaleType(viewHolder, width, height);
                        return false;
                    }
                })
                .into(viewHolder.sdvCover);

        Glide.with(mContext)
                .load(videoBean.getUserLogo())
                .apply(new RequestOptions().error(R.drawable.comment_head_default).placeholder(R.drawable.comment_head_default))
                .into(viewHolder.headIcon);

        viewHolder.nickName.setText(videoBean.getNickname());
        viewHolder.summary.setText(videoBean.getInfo());
        if (StringUtils.isNotEmpty(videoBean.getTitle())) {
            viewHolder.tagName.setText(videoBean.getTitle());
        }

        viewHolder.commentNum.setText(StringUtils.videoNumTranslate(videoBean.getCommentNumber()));
        viewHolder.likeNum.setText(StringUtils.videoNumTranslate(videoBean.getThumbsCount()));
        viewHolder.ForwardNum.setText(StringUtils.videoNumTranslate(videoBean.getShareCount()));

//        viewHolder.headIcon.setOnClickListener(v -> {
//            Bundle bundle = new Bundle();
//            bundle.putString("id", videoBean.getActivityId() + "");
//            bundle.putString("userName", videoBean.getUsername());
//            RouterManager.router(ARouterPathConstant.SHORT_VIDEO_USER,bundle);
//        });

//        viewHolder.tagName.setOnClickListener(v -> {
//            Bundle bundle = new Bundle();
//            bundle.putString("id", videoBean.getTagId() + "");
//            bundle.putString("actId", videoBean.getActivityId() + "");
//            RouterManager.router(ARouterPathConstant.SHORT_VIDEO_THEME_DETAIL, bundle);
//        });

        viewHolder.videoLayout.setOnClickListener(v -> {
            if (viewHolder.videoView != null) {
                if (viewHolder.videoView.isPlaying()) {
                    viewHolder.videoView.pause();
                    viewHolder.videoTag.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.videoView.start();
                    viewHolder.videoTag.setVisibility(View.GONE);
                }
            }
        });

        if (videoBean.getIsLike() == 1) {
            viewHolder.likeNum.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(mContext, R.drawable.micro_video_like_on), null, null);
        } else {
            viewHolder.likeNum.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(mContext, R.drawable.micro_video_like_off), null, null);
        }

        viewHolder.commentNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemChildClickListener.onClickListener(viewHolder.commentNum, videoBean, position);
            }
        });

        viewHolder.likeNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemChildClickListener.onClickListener(viewHolder.likeNum, videoBean, position);
            }
        });


        viewHolder.ForwardNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemChildClickListener.onClickListener(viewHolder.ForwardNum, videoBean, position);
            }
        });

    }


    private void fitVideoScaleType(VideoViewHolder viewHolder, int width, int height) {
        int contentWidth = recyclerView.getWidth();
        int contentHeight = recyclerView.getHeight();
        VideoUtils.ScaleType scaleType = VideoUtils.getImageCropType(new Pair<>(contentWidth, contentHeight),
                new Pair<>(width, height));
        if (scaleType == VideoUtils.ScaleType.CENTER_CROP) {
            viewHolder.sdvCover.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else if (scaleType == VideoUtils.ScaleType.FIT_CENTER) {
            viewHolder.sdvCover.setScaleType(ImageView.ScaleType.FIT_CENTER);
        }
    }


    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public void addData(List<ShortVideoBean> newDataList) {
        this.dataList.addAll(newDataList);
    }

    public void setData(List<ShortVideoBean> newDataList) {
        this.dataList.clear();
        this.dataList.addAll(newDataList);
        notifyDataSetChanged();
    }

    public ShortVideoBean getDataByPosition(int position) {
        return dataList.get(position);
    }

    public class VideoViewHolder extends RecyclerView.ViewHolder {
        public ListVideoView videoView;
        public ImageView sdvCover, videoTag;
        RoundedImageView headIcon;
        public TextView nickName, summary, tagName, comment, ForwardNum, commentNum, likeNum;
        public ProgressBar progressBar;
        RelativeLayout videoLayout;


        VideoViewHolder(View itemView) {
            super(itemView);
            videoView = itemView.findViewById(R.id.video_view);
            sdvCover = itemView.findViewById(R.id.video_cover);
            headIcon = itemView.findViewById(R.id.head_icon);
            progressBar = itemView.findViewById(R.id.progress_bar);
            nickName = itemView.findViewById(R.id.nickName);
            summary = itemView.findViewById(R.id.summary);
            tagName = itemView.findViewById(R.id.tagName);
            ForwardNum = itemView.findViewById(R.id.forward_num);
            videoTag = itemView.findViewById(R.id.video_tag);
            videoLayout = itemView.findViewById(R.id.video_layout);
            commentNum = itemView.findViewById(R.id.comment_num);
            likeNum = itemView.findViewById(R.id.like_num);
        }
    }
}
